﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using DTO;
using BLL;


namespace HDT.GUI
{
    public partial class FrmThemNV : Form
    {
        private string ACCOUNT_IMAGE_FOLDER = "Image\\_Account\\";
        public delegate void Mydelegate();
        public Mydelegate refreshForm;
        private List<LoaiNhanVienDTO> dsLoaiNV;
        Image staffImage;

        public FrmThemNV()
        {
            InitializeComponent();
            picNV.Image = picNV.ErrorImage;
            staffImage = null;
        }

        public void LoadInfoLoaiNV()
        {
            try
            {
                dsLoaiNV = new LoaiNhanVienBLL().Select();
                foreach (LoaiNhanVienDTO loaiNV in dsLoaiNV)
                {
                    cboLoaiNV.Items.Add(loaiNV.TenLoaiNV);
                }
                cboLoaiNV.SelectedIndex = 0;
            }
            catch
            { }
        }

        private void picNV_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bit = new Bitmap(open.FileName);
                    staffImage = bit;
                }
            }
            catch
            {
                MessageBox.Show("Không thể mở được hình?", "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
                staffImage = null;
            }
            picNV.Image = staffImage;
        }

        private void FrmThemNV_Load(object sender, EventArgs e)
        {
            LoadInfoLoaiNV();
            picNV.Image = staffImage;
            dtmNgaySinh.Value = DateTime.Now.AddYears(-18);
        }

        private void btnQuayLai_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            bool status = !("".Equals(txtHoTen.Text.Trim()) || "".Equals(txtDiaChi.Text.Trim())
                || "".Equals(txtEmail.Text.Trim()) || "".Equals(txtSDT.Text.Trim()) || "".Equals(txtCMND.Text.Trim()));
            btnThemNV.Enabled = status;
            btnNhapLai.Enabled = status;
        }

        private void btnThemLNV_Click(object sender, EventArgs e)
        {
            FrmThemLoaiNV frm_ThemLoaiNV = new FrmThemLoaiNV();
            frm_ThemLoaiNV.ShowDialog();
        }

        private void SaveStaffImage(NhanVienDTO nhanvien)
        {
            try
            {
                string path = ACCOUNT_IMAGE_FOLDER + nhanvien.MaNhanVien + ".bmp";
                staffImage.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
                nhanvien.HinhAnh = nhanvien.MaNhanVien + ".bmp";
                new NhanVienBLL().Update(nhanvien);
            }
            catch
            { }
        }

        private void NewNV(ref NhanVienDTO nhanvien)
        {
            nhanvien.HoTen = txtHoTen.Text.Trim();
            nhanvien.NgaySinh = dtmNgaySinh.Value;
            nhanvien.DiaChi = txtDiaChi.Text.Trim();
            nhanvien.Email = txtEmail.Text;
            nhanvien.DienThoai = txtSDT.Text;
            nhanvien.CMND = txtCMND.Text;
            nhanvien.MaLoaiNhanVien = dsLoaiNV[cboLoaiNV.SelectedIndex].MaLoaiNhanVien;
            new NhanVienBLL().Insert(nhanvien);
            nhanvien = new NhanVienBLL().GetLast();
        }

        private void btnThemNV_Click(object sender, EventArgs e)
        {
            try
            {
                NhanVienDTO nhanvien = new NhanVienDTO();
                NewNV(ref nhanvien);
                SaveStaffImage(nhanvien);
                new TKDangNhapBLL().SetDefaultPassword(new TKDangNhapBLL().GetLast());
                MessageBox.Show("Thêm nhân viên mới thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                refreshForm();
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSuaLNV_Click(object sender, EventArgs e)
        {
            FrmSuaLoaiNV frm_SuaLoaiNV = new FrmSuaLoaiNV();
            frm_SuaLoaiNV.ShowDialog();
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            txtHoTen.Clear();
            dtmNgaySinh.Value = DateTime.Now.AddYears(-18);
            txtDiaChi.Clear();
            txtEmail.Clear();
            txtSDT.Clear();
            txtCMND.Clear();
        }

        private void number_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
